package com.aoasaa;
import android.app.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;

import android.net.Uri;
import android.os.Bundle;

import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import android.widget.AdapterView.OnItemClickListener;
/*This class is used to browse the images from Sdcard and display on the emulator*/
public class Browsesdcardimage extends Activity {
	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
 
	private Cursor cursor;
	private int columnIndex;
	public SharedPreferences sp_obj;
	public Editor editor_obj;
	//UploadFile uploadFile_obj;
	String uploadcheck;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browseimage);
		try{
			sp_obj=getSharedPreferences("ArchiveApp",MODE_WORLD_WRITEABLE);  
			editor_obj=sp_obj.edit(); 
			// Set up an array of the Thumbnail Image ID column we want
			String[] projection = {MediaStore.Images.Thumbnails._ID};

			// Create the cursor pointing to the SDCard
			cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
					projection, // Which columns to return
					null,       // Return all rows
					null,
					MediaStore.Images.Thumbnails.IMAGE_ID);
			// Get the column index of the Thumbnails Image ID
			//	columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
			GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
			sdcardImages.setAdapter(new ImageAdapter(this));

			// Set up a click listener
			sdcardImages.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView parent, View v, int position, long id) {
					// Get the data location of the image
					String[] projection = {MediaStore.Images.Media.DATA};
					cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
							projection, // Which columns to return
							null,       // Return all rows
							null,
							null);
					columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
					cursor.moveToPosition(position);
					String imagePath = cursor.getString(columnIndex);

					String[] titles_data={MediaStore.Images.Media.DISPLAY_NAME};
					Cursor cursor1 = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
							titles_data, // Which columns to return
							null,       // Return all rows
							null,
							null);
					int selected_index=cursor1.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
					cursor1.moveToPosition(position);
					String selected_file=cursor1.getString(selected_index);
					// Get image filename
 
					Toast.makeText(getApplicationContext(), ""+imagePath, Toast.LENGTH_LONG);
					editor_obj.putString("SelectedFile",selected_file);
					editor_obj.putString("uploadsessionfileurl", imagePath);
					editor_obj.commit(); 
					
					// using intent to navigate to uploading activity
					
					Intent obj=new Intent(Browsesdcardimage.this,UploadingImageNotes.class);
					startActivity(obj); 
					
					
//					uploadFile_obj=new UploadFile();
//					uploadcheck=uploadFile_obj.uploadingMethod(imagePath);
//					editor_obj.putString("UploadCheck",uploadcheck);
//					editor_obj.commit();
					//finish();
					// Use this path to do further processing, i.e. full screen display
				}
			});
		}  
		catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	/**
	           .* Adapter for our image files.
	 */
	private class ImageAdapter extends BaseAdapter {

		private Context context;

		public ImageAdapter(Context localContext) {
			context = localContext;
		}

		public int getCount() {
			return cursor.getCount();
		}
		public Object getItem(int position) {
			return position;
		}
		public long getItemId(int position) {
			return position;
		}
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView picturesView;
			if (convertView == null) {
				picturesView = new ImageView(context);
				// Move cursor to current position
				cursor.moveToPosition(position);
				// Get the current value for the requested column
				int imageID = cursor.getInt(columnIndex);
				// Set the content of the image based on the provided URI
				picturesView.setImageURI(Uri.withAppendedPath(
						MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
				picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
				picturesView.setPadding(8, 8, 8, 8);
//				picturesView.setMinimumHeight(40);
//				picturesView.setMaxWidth(40);
				picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
			}
			else {
				picturesView = (ImageView)convertView;
			}
			return picturesView;
		};


	}

	//	@Override
	//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	//		// TODO Auto-generated method stub
	//		if(requestCode==333&&(resultCode==RESULT_OK || resultCode==0))
	//			finish();
	//		super.onActivityResult(requestCode, resultCode, data);
	//	}

}


